package com.juyu.miliao.scheduler.message;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.common.constant.SpringFestivalActivitiesRabbitMqInfo;
import com.juyu.miliao.scheduler.domin.GiftSpend;
import com.juyu.miliao.scheduler.service.SpringFestivalActivitiesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * 春节活动信息
 *
 * @author xuanqicheng
 * @create 2019-28-16:41
 */
@Component
@Slf4j
public class SpringFestivalActivitiesMessage {

    @Autowired
    private SpringFestivalActivitiesService springFestivalActivitiesService;

    private  LocalDateTime startTime = LocalDateTime.of(2019, 1, 31, 23, 59, 59);

    private LocalDateTime endTime = LocalDateTime.of(2019, 2, 16, 0, 0, 0);

    @RabbitListener(bindings = @QueueBinding(value = @Queue(SpringFestivalActivitiesRabbitMqInfo.SPRING_FESTIVAL_QUEUE),
            exchange = @Exchange(SpringFestivalActivitiesRabbitMqInfo.SPRING_FESTIVAL_EXCHANGE),
            key = SpringFestivalActivitiesRabbitMqInfo.SPRING_FESTIVAL_KEY))
    public void getGiftWallMessage(String message) {
        if (!StringUtils.isEmpty(message)) {
            try {
                GiftSpend giftSpend = JSONObject.parseObject(message, GiftSpend.class);
                if(giftSpend.getCreateTime().isAfter(startTime)&&giftSpend.getCreateTime().isBefore(endTime)){
                    springFestivalActivitiesService.setSpringFestivalActivitiesInfo(giftSpend);
                }
            } catch (Exception ex) {
                log.error("处理春节活动原始数据错误：" + ex.getMessage());
            }
        }
    }

}
